package com.cjh.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日历工具类
 */
public class CalendarUtil {

    /**
     * 获取明天日期（yyyy-MM-dd）
     */
    public static String getTomorrowDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        String tomorrowDate = format.format(c.getTime());
        return tomorrowDate;
    }

    /**
     * 获取年月日
     * @param date
     */
    public static Map<String,Integer> getYearMonthDay(Date date){

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Map<String,Integer> data=new HashMap<>();
        data.put("year",year);
        data.put("month",month);
        data.put("day",day);
        return data;
    }

//    /**
//     * 减少8小时
//     * @param measureTargetVo
//     */
//    public static void reduceEightHour(MeasureTargetVo measureTargetVo){
//
//        //将日期减少8小时
//        Calendar beginCalendar = Calendar.getInstance();
//        beginCalendar.setTime(measureTargetVo.getBeginDate());
//        beginCalendar.add(Calendar.HOUR_OF_DAY, -8);
//        measureTargetVo.setBeginDate(beginCalendar.getTime());
//        Calendar endCalendar = Calendar.getInstance();
//        endCalendar.setTime(measureTargetVo.getEndDate());
//        endCalendar.add(Calendar.HOUR_OF_DAY, -8);
//        measureTargetVo.setEndDate(endCalendar.getTime());
//    }

    /**
     * 根据日期获取该日期是该年的多少天
     * @param date
     * @return
     */
    public static Integer getDayNumForYear(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        return ca.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 判断传入的日期是否是昨天
     * @param date
     * @return  true为昨天
     */
    public static boolean isYesterday(Date date){
        boolean flag = false;
        // 先获取年份
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));

        // 获取当前年份 和 一年中的第几天
        int day =  CalendarUtil.getDayNumForYear(date);
        int currentYear = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
        Integer currentDay = CalendarUtil.getDayNumForYear(new Date());
        // 计算 如果是去年的
        if (currentYear - year == 1) {
            // 如果当前正好是 1月1日 计算去年有多少天，指定时间是否是一年中的最后一天
            if (currentDay == 1) {
                int yearDay;
                if (year % 400 == 0) {
                    // 世纪闰年
                    yearDay = 366;
                } else if (year % 4 == 0 && year % 100 != 0) {
                    // 普通闰年
                    yearDay = 366;
                } else {
                    // 平年
                    yearDay = 365;
                }
                if (day == yearDay) {
                    flag = true;
                }
            }
        } else {
            if (currentDay - day == 1) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 获取今天开始时间
     */
    public static Date getTodayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取今天结束时间
     */
    public static Date getTodayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 获取昨天开始时间
     */
    public static Date getYesterdayStartTime() {
        Calendar yesterdayStart = Calendar.getInstance();
        yesterdayStart.add(Calendar.DAY_OF_MONTH,-1);
        yesterdayStart.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayStart.set(Calendar.MINUTE, 0);
        yesterdayStart.set(Calendar.SECOND, 0);
        yesterdayStart.set(Calendar.MILLISECOND, 0);
        return yesterdayStart.getTime();
    }

    /**
     * 获取昨天结束时间
     */
    public static Date getYesterdayEndTime() {
        Calendar yesterdayEnd = Calendar.getInstance();
        yesterdayEnd.add(Calendar.DAY_OF_MONTH,-1);
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 23);
        yesterdayEnd.set(Calendar.MINUTE, 59);
        yesterdayEnd.set(Calendar.SECOND, 59);
        yesterdayEnd.set(Calendar.MILLISECOND, 999);
        return yesterdayEnd.getTime();
    }

    /**
     * 获取本周开始时间
     */
    public static Date getCurrentWeekStartTime() {
        Calendar currentWeekStart = Calendar.getInstance();
        currentWeekStart.setFirstDayOfWeek(Calendar.MONDAY);
        currentWeekStart.set(Calendar.HOUR_OF_DAY, 0);
        currentWeekStart.set(Calendar.MINUTE, 0);
        currentWeekStart.set(Calendar.SECOND, 0);
        currentWeekStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return currentWeekStart.getTime();
    }

    /**
     * 获取本周结束时间
     */
    public static Date getCurrentWeekEndTime() {
        Calendar currentWeekEnd = Calendar.getInstance();
        currentWeekEnd.setFirstDayOfWeek(Calendar.MONDAY);
        currentWeekEnd.set(Calendar.HOUR_OF_DAY, 23);
        currentWeekEnd.set(Calendar.MINUTE, 59);
        currentWeekEnd.set(Calendar.SECOND, 59);
        currentWeekEnd.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return currentWeekEnd.getTime();
    }

    /**
     * 获取当前月开始时间（第一天 yyyy-MM-dd）
     */
    public static String getCurrentMonthStartDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        return first;
    }

    /**
     * 获取当前月开始时间（第一天）
     */
    public static Date getCurrentMonthStartTime() {
        Calendar currentMonthStart = Calendar.getInstance();
        currentMonthStart.set(Calendar.DAY_OF_MONTH,1);
        currentMonthStart.set(Calendar.HOUR_OF_DAY, 0);
        currentMonthStart.set(Calendar.MINUTE, 0);
        currentMonthStart.set(Calendar.SECOND, 0);
        currentMonthStart.set(Calendar.MILLISECOND, 0);
        return currentMonthStart.getTime();
    }

    /**
     * 获取当前月结束时间（最后一天）
     */
    public static Date getCurrentMonthEndTime() {
        Calendar currentMonthEnd = Calendar.getInstance();
        currentMonthEnd.set(Calendar.DAY_OF_MONTH, currentMonthEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
        currentMonthEnd.set(Calendar.HOUR_OF_DAY, 23);
        currentMonthEnd.set(Calendar.MINUTE, 59);
        currentMonthEnd.set(Calendar.SECOND, 59);
        currentMonthEnd.set(Calendar.MILLISECOND, 999);
        return currentMonthEnd.getTime();
    }

    /**
     * 将日期增加时分秒（00:00:00）
     */
    public static Date getHourMinSecondBegin(Date date){
        Calendar calendarBegin=Calendar.getInstance();
        calendarBegin.setTime(date);
        calendarBegin.set(Calendar.HOUR_OF_DAY,0);
        calendarBegin.set(Calendar.MINUTE,0);
        calendarBegin.set(Calendar.SECOND,0);
        return calendarBegin.getTime();
    }

    /**
     * 将日期增加时分秒（23:59:59）
     */
    public static Date getHourMinSecondEnd(Date date){
        Calendar calendarEnd=Calendar.getInstance();
        calendarEnd.setTime(date);
        calendarEnd.set(Calendar.HOUR_OF_DAY,23);
        calendarEnd.set(Calendar.MINUTE,59);
        calendarEnd.set(Calendar.SECOND,59);
        return calendarEnd.getTime();
    }

    /**
     * 根据日期查询当前月有多少天
     * @return
     */
    public static int getDaysByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 将excel数字转为日期
     * @param use1904windowing
     * @param value
     * @return
     */
    public static String getDate(boolean use1904windowing,double value){

        int wholeDays = (int)Math.floor(value);
        int millisecondsInDay = (int)((value - (double)wholeDays) * 8.64E7D + 0.5D);

        Calendar calendar = new GregorianCalendar();

        short startYear = 1900;
        byte dayAdjust = -1;

        if (use1904windowing) {
            startYear = 1904;
            dayAdjust = 1;
        }else if (wholeDays < 61) {
            dayAdjust = 0;
        }
        calendar.set(startYear, 0, wholeDays + dayAdjust, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, millisecondsInDay);

        if(calendar.get(Calendar.MILLISECOND) == 0) {
            calendar.clear(Calendar.MILLISECOND);
        }

        Date date = calendar.getTime();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        return s.format(date);
    }

    /**
     * 根据起始时间和终止时间获取区间的时间集合
     * @param beginDate
     * @param endDate
     * @return
     */
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate)  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> lDate = new ArrayList<>();
        try {
            lDate.add(beginDate);//把开始时间加入集合
            Calendar cal = Calendar.getInstance();
            //使用给定的 Date 设置此 Calendar 的时间
            cal.setTime(sdf.parse(beginDate));
            boolean bContinue = true;
            while (bContinue) {
                //根据日历的规则，为给定的日历字段添加或减去指定的时间量
                cal.add(Calendar.DAY_OF_MONTH, 1);
                // 测试此日期是否在指定日期之后
                if (sdf.parse(endDate).after(cal.getTime())) {
                    lDate.add(sdf.format(cal.getTime()));
                } else {
                    break;
                }
            }
            lDate.add(endDate);//把结束时间加入集合
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return lDate;
    }

}